﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCDB.Entity;
using FCDB.Repository;
using FCDB.Web.Models;
using FB.Mvc.Controllers;
using FCDB.Entity.Enum;

namespace FCDB.Web.Controllers
{
      [FB.Membership.Annotations.AuthorizeOnly]
    public class WorkFlowTypeController : BaseController
    {
        //
        // GET: /WorkFlowType/
        private WFWorkFlowRepository respository = new WFWorkFlowRepository();
        public ActionResult Index()
        {
            if (Request.IsAjaxRequest())
            { 
                IList<WfProcessType> list = respository.GetAll<WfProcessType>();
                PagerModel model = new PagerModel()
                {
                    Data = list,
                    TotalCount = list.Count()
                };
                JsonResult json = new JsonResult()
                {
                    JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    Data = model
                }; 
                return json;
            } 
            return View(); 
        }

        public ActionResult Create()
        {
            if (string.IsNullOrEmpty(Request["ID"]))
            {
                WfProcessType type = new WfProcessType();
                return View(type);
            }
            else
            {
                string id = Request["ID"];
                WfProcessType type = respository.Get<WfProcessType>(long.Parse(id));
                return View(type);
            } 
        }
        public ActionResult Save(WfProcessType model)
        {
            RtnMsg msg = new RtnMsg() { Status = 1 };
            if (model == null)
            {
                msg.Status = 3;
                return Json(msg);
            }
            if (model.ID == 0)
            { 
                model.CreateTime = DateTime.Now;
                model.Status = (int)EntityStatus.Normal;
                model.ID = IDCreateLogic.GenerateID("IWFProcessType", "流程类型");
                model.CreateUserID = UserID; 
                respository.Add(model);
                return Json(msg);
            }
            WfProcessType updatemodel = respository.Get<WfProcessType>(model.ID);
            updatemodel.SortNo = model.SortNo;
            updatemodel.Remark = model.Remark;
            updatemodel.Name = model.Name;   
            updatemodel.UpdateTime = DateTime.Now;
            updatemodel.UpdateUserID = UserID;
            updatemodel.Code = model.Code;
            respository.Update(updatemodel);
            return Json(msg);
        }
        public ActionResult Delete(string rpValue)
        {
            string[] ids = rpValue.Split(",".ToArray());
            if (ids.Length == 0) return null;
            foreach (string item in ids)
            { 
                if (!string.IsNullOrEmpty(item))
                {
                   WfProcessType type=   respository.Get<WfProcessType>(long.Parse(item));
                   type.Status = (int)EntityStatus.Deleted;
                   type.UpdateTime = DateTime.Now;
                   type.UpdateUserID = UserID;
                   respository.Update(type);
                    
                }
            }
            
            return null; 
        }
    }
}
